<!DOCTYPE html>
<html class="x-admin-sm" lang="en"
      xmlns:th="https://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>员工技能列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style type="text/css">
        .input-tar{
            float: left;
            font-size: 10px;
            padding: 3px 4px;
            margin-right: 4px;
            margin-top: 6px;
            border-radius: 2px;
            line-height: 13px;
            color: #fff;
        }
        .rqcode{
            margin: 0 auto;
            width: 30px;
            height: 30px;
        }
    </style>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">二维码管理</a>
                <a>
                    <cite>员工技能</cite>
                </a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form" action="" lay-filter="query_employee_form">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="employeeNumber" placeholder="请输入工号" autocomplete="off"
                                   class="layui-input" id="employeeNumber">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="employeeName" placeholder="请输入员工姓名" autocomplete="off"
                                   class="layui-input" id="employeeName">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i>查询</button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <shiro:hasPermission name="p_person_skill_add">
                    <button class="layui-btn layui-btn-normal" onclick="openAddSopPosition()">
                        <i class="layui-icon">&#xe642;</i>新增员工
                    </button>
                    </shiro:hasPermission>
                    <button class="layui-btn layui-btn-warm" onclick="downloadQrcode()">
                        <i class="layui-icon">&#xe601;</i>下载二维码
                    </button>
                    <shiro:hasPermission name="p_person_skill_import">
                    <button class="layui-btn layui-btn-danger" onclick="openUpload()">
                        <i class="layui-icon">&#xe601;</i>导入
                    </button>
                    </shiro:hasPermission>
                    <table class="layui-table" lay-filter="employee-table" id="table1">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script th:src="@{/assets/admin/js/jquery.min.js}"></script>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>
<script type="text/html" id="switchStatus">
    <!--这里的checked的状态只是演示-->
    <input type = "checkbox" name = "status" value = "{{ d.id }}" lay-skin = "switch"lay-text = "正常|停用" lay-filter = "" {{ d.status == 1 ? 'checked':'' }}>
</script>

<script type="text/html" id="barDemo">
    <shiro:hasPermission name="p_person_skill_add">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    </shiro:hasPermission>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="create_qrcode">生成二维码</a>
    <shiro:hasPermission name="p_person_skill_delete">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </shiro:hasPermission>
</script>

<shiro:hasRole name="admin">
    <script type="text/javascript">
        var opWidth = 230;
    </script>
</shiro:hasRole>
<script type="text/javascript" th:inline="javascript">

    let userTable = null;
    let tarBgc = ["#FF5722","#FFB800","#009688","#2F4056","#1E9FFF" ,"#393D49"];
    $(function () {
    });

    if(opWidth == null || opWidth == undefined || opWidth == ''){
        var opWidth = 185;
    }

    layui.use(['jquery','laydate', 'table', 'form'],
        function () {
            laydate = layui.laydate;
            table = layui.table;
            form = layui.form;
            $ = layui.jquery;

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });

            //第一个实例
            userTable = table.render({
                elem: '#table1',
                url: '/qrcode/getEmployeeSkillByPage', //数据接口
                id:"employee_table_id",
                method: "post",
                where: {     //请求参数

                },
                parseData: function (res) { //res 即为原始返回的数据
                    var data = res.result;
                    return {
                        "code": res.code, //解析接口状态
                        // "msg": res.message, //解析提示文本
                        "count": data.total, //解析数据长度
                        "data": data.records //解析数据列表
                    };
                },
                request:{
                    pageName:"pageNum",
                    limitName:"pageSize"
                },
                response: {//重新定义规则
                    //statusName: 'status', //规定数据状态的字段名称，默认：code
                    statusCode: 200, //规定成功的状态码，默认：0
                    //msgName: 'hint', //规定状态信息的字段名称，默认：msg
                    //countName: 'total', //规定数据总数的字段名称，默认：count
                    //dataName: 'rows' //规定数据列表的字段名称，默认：data
                },
                page: true, //开启分页
                limits: [5 ,10, 20, 50 , 100],//可选条目数
                toolbar: true,//若需要“列显示隐藏”、“导出”、“打印”等功能，则必须开启该参数
                loading: true, //家在动画
                cols: [
                    [ //表头
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'employeeNumber', title: '工号',width:150},
                        {field: 'employeeName', title: '姓名', width: 100 },
                        {field: 'position', title: '岗位', width: 200, templet: function(data){
                                let positions = data.position;
                                if(positions == null || positions == undefined){
                                    return '';
                                }
                                let result = "";
                                for (let i=0;i< positions.length ; i++){
                                    // let gbcIndex =  i% tarBgc.length;
                                    let gbcIndex = Math.floor(Math.random() * tarBgc.length);

                                    result += "<span class='input-tar' style='background-color:"+tarBgc[gbcIndex]+"'>"+positions[i]+"</span>"
                                }
                                return result;
                            }
                        },
                        {field: 'remarks', title: '备注', width:120},
                        {field: 'qrCodePath', title: '二维码', width:80 ,unresize: true,templet:function(data){
                            return "<div id='qrcode"+data.id+"' class='rqcode'><img style='margin:0 auto;width:30px;height:30px;' src='"+data.qrCodePath+"'></div>"
                            }
                        },
                        {field: 'createTime', title: '创建时间',width:145,sort: true,templet : "<div>{{layui.util.toDateString(d.createTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"},
                        {field: 'createBy', title: '创建人',width:145},
                        {field: 'operation', title:'操作', toolbar: '#barDemo',fixed: 'right',width: opWidth}
                    ]
                ],
                done:function(){
                    $(".rqcode").mouseover(function(){
                        let src = "<img style='width: 150px;height:150px;' src="+$(this).find("img").attr("src")+" />";
                        let tips_index = layer.tips(src,this,{
                            time: 0
                        });
                        console.log(tips_index);
                        $(this).attr("tips_id",tips_index);
                        //console.log($(this).find("img").attr("src"));
                    });
                    $(".rqcode").mouseout(function(){
                        layer.close($(this).attr("tips_id"));
                        $(this).removeAttr("tips_id");
                    });
                }
            });

            //必须使用单引号。过分坑啊！！！！
            form.on('submit(sreach)', function (data) {
                refreshData(1);
                return false;
            });

            //监听行工具事件  这个employee-table是指，<table> 属性lay-filter = 'employee-table'
            table.on('tool(employee-table)', function (obj) {
                //当前行的数据
                let data = obj.data;
                //删除
                if(obj.event === "del"){
                    layer.confirm("确定删除吗？",function(index){
                        $.ajax({
                            url: "/qrcode/deletePersonById",
                            type: "DELETE",
                            data: {"personId": data.id},
                            dataType: "json",
                            success: function (data) {
                                refreshData();
                                if (data.code == 200) {
                                    layer.alert(data.message, {icon: 6}, function (iIndex) {
                                        layer.close(iIndex);
                                    });
                                } else {
                                    layer.alert(data.message, {icon: 5}, function (iIndex) {
                                        layer.close(iIndex);
                                    });
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
                //编辑
                else if(obj.event === "edit"){
                    openLayer("编辑员工技能信息","/qrcode/eidtEmployeeSkillUI?employeeId="+data.id,450,550,false, function(){refreshData();});
                }
                //生成二维码
                else if(obj.event == "create_qrcode"){
                    //弹出确认框
                    layer.confirm("是否要生成该二维码图片？",function(index){
                        $.ajax({
                            url: "/qrcode/createEmployeeSkillQrcode",
                            type: "POST",
                            data: {"employeeId": data.id},
                            dataType: "json",
                            success: function (data) {
                                refreshData();
                                if (data.code == 200) {
                                    layer.alert(data.message, {icon: 6}, function (iIndex) {
                                        layer.close(iIndex);
                                    });
                                } else {
                                    layer.alert(data.message, {icon: 5}, function (iIndex) {
                                        layer.close(iIndex);
                                    });
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
            });

    //点击行checkbox选中
    $(document).on("click", ".layui-table-body table.layui-table tbody tr", function() {
        var index = $(this).attr('data-index');
        var tableBox = $(this).parents('.layui-table-box');
        //存在固定列
        if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
            tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
        } else {
            tableDiv = tableBox.find(".layui-table-body.layui-table-main");
        }
        var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
        if (checkCell.length > 0) {
            checkCell.click();
        }
    });

    $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function(e) {
        e.stopPropagation();
    });

    //监听复选框事件，被选中的行高亮显示
    table.on('checkbox(employee-table)', function (obj) {
        if (obj.checked == true && obj.type == 'all') {
            //点击全选
            $('.layui-table-body table.layui-table tbody tr').addClass('layui-table-click');
        } else if (obj.checked == false && obj.type == 'all') {
            //点击全不选
            $('.layui-table-body table.layui-table tbody tr').removeClass('layui-table-click');
        } else if (obj.checked == true && obj.type == 'one') {
            //点击单行
            if (obj.checked == true) {
                obj.tr.addClass('layui-table-click');
            } else {
                obj.tr.removeClass('layui-table-click');
            }
        } else if (obj.checked == false && obj.type == 'one') {
            //点击全选之后点击单行
            if (obj.tr.hasClass('layui-table-click')) {
                obj.tr.removeClass('layui-table-click');
            }
        }
    });

});
    //刷新列表信息
    function refreshData(page) {
        if(isEmpty(page)){
            page = $(".layui-laypage-em").next().html();
        }
        //获取form表单的数据，query_employee_form 是 <form lay-filter="query_employee_form">
        var data = form.val("query_employee_form");

        userTable.reload({
            page: {
                curr: page  //主要代码行
            },
            where: data
        });
    }

    function openAddSopPosition(){
        openLayer("新增员工技能信息","/qrcode/eidtEmployeeSkillUI",450,550,false,function(){refreshData();});
    }
    function openUpload(){
        openLayer("导入员工技能信息","/qrcode/uploadEmployeeSkillUI",400,280,false,function(){refreshData();});
    }

    function downloadQrcode(){
        let data = form.val("query_employee_form");
        let param = JSON.stringify(data);
        let getParams = json_to_get(param);
        /**
         *获取勾选的数据
         */
        let datas = table.checkStatus("employee_table_id").data;
        if(datas!= null && datas.length>0){
            $.each(datas,function(index,item){
                getParams +="&ids="+item.id;
            })
        }
        window.open('/qrcode/downloadEmployeeSkillQrcode?'+getParams);
    }
</script>
</html>